home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50a Issue 142 (CD142a) (August 1998).iso / trial / demon / TURNPIKE.1 / CLASSES.ZIP / JAVA / IO / PipedInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-04-14  |  1.8 KB  |  153 lines

  1. package java.io;
  2.  
  3. public class PipedInputStream extends InputStream {
  4.    boolean closed = true;
  5.    Thread readSide;
  6.    Thread writeSide;
  7.    private byte[] buffer = new byte[1024];
  8.    // $FF: renamed from: in int
  9.    int field_0 = -1;
  10.    int out;
  11.  
  12.    public PipedInputStream(PipedOutputStream var1) throws IOException {
  13.       this.connect(var1);
  14.    }
  15.  
  16.    public PipedInputStream() {
  17.    }
  18.  
  19.    public void connect(PipedOutputStream var1) throws IOException {
  20.       var1.connect(this);
  21.    }
  22.  
  23.    synchronized void receive(int var1) throws IOException {
  24.       this.writeSide = Thread.currentThread();
  25.  
  26.       while(this.field_0 == this.out) {
  27.          if (this.readSide != null && !this.readSide.isAlive()) {
  28.             throw new IOException("Pipe broken");
  29.          }
  30.  
  31.          this.notifyAll();
  32.  
  33.          try {
  34.             this.wait(1000L);
  35.          } catch (InterruptedException var2) {
  36.             throw new InterruptedIOException();
  37.          }
  38.       }
  39.  
  40.       if (this.field_0 < 0) {
  41.          this.field_0 = 0;
  42.          this.out = 0;
  43.       }
  44.  
  45.       this.buffer[this.field_0++] = (byte)(var1 & 255);
  46.       if (this.field_0 >= this.buffer.length) {
  47.          this.field_0 = 0;
  48.       }
  49.  
  50.    }
  51.  
  52.    synchronized void receive(byte[] var1, int var2, int var3) throws IOException {
  53.       while(true) {
  54.          --var3;
  55.          if (var3 < 0) {
  56.             return;
  57.          }
  58.  
  59.          this.receive(var1[var2++]);
  60.       }
  61.    }
  62.  
  63.    synchronized void receivedLast() {
  64.       this.closed = true;
  65.       this.notifyAll();
  66.    }
  67.  
  68.    public synchronized int read() throws IOException {
  69.       int var1 = 2;
  70.  
  71.       while(this.field_0 < 0) {
  72.          this.readSide = Thread.currentThread();
  73.          if (this.writeSide != null && !this.writeSide.isAlive()) {
  74.             --var1;
  75.             if (var1 < 0) {
  76.                throw new IOException("Pipe broken");
  77.             }
  78.          }
  79.  
  80.          if (this.closed) {
  81.             return -1;
  82.          }
  83.  
  84.          this.notifyAll();
  85.  
  86.          try {
  87.             this.wait(1000L);
  88.          } catch (InterruptedException var3) {
  89.             throw new InterruptedIOException();
  90.          }
  91.       }
  92.  
  93.       int var2 = this.buffer[this.out++] & 255;
  94.       if (this.out >= this.buffer.length) {
  95.          this.out = 0;
  96.       }
  97.  
  98.       if (this.field_0 == this.out) {
  99.          this.field_0 = -1;
  100.       }
  101.  
  102.       return var2;
  103.    }
  104.  
  105.    public synchronized int read(byte[] var1, int var2, int var3) throws IOException {
  106.       if (var3 <= 0) {
  107.          return 0;
  108.       } else {
  109.          int var4 = this.read();
  110.          if (var4 < 0) {
  111.             return -1;
  112.          } else {
  113.             var1[var2] = (byte)var4;
  114.             int var5 = 1;
  115.  
  116.             while(this.field_0 >= 0) {
  117.                --var3;
  118.                if (var3 <= 0) {
  119.                   break;
  120.                }
  121.  
  122.                var1[var2 + var5] = this.buffer[this.out++];
  123.                ++var5;
  124.                if (this.out >= this.buffer.length) {
  125.                   this.out = 0;
  126.                }
  127.  
  128.                if (this.field_0 == this.out) {
  129.                   this.field_0 = -1;
  130.                }
  131.             }
  132.  
  133.             return var5;
  134.          }
  135.       }
  136.    }
  137.  
  138.    public synchronized int available() throws IOException {
  139.       if (this.field_0 < 0) {
  140.          return 0;
  141.       } else if (this.field_0 == this.out) {
  142.          return this.buffer.length;
  143.       } else {
  144.          return this.field_0 > this.out ? this.field_0 - this.out : this.field_0 + this.buffer.length - this.out;
  145.       }
  146.    }
  147.  
  148.    public void close() throws IOException {
  149.       this.field_0 = -1;
  150.       this.closed = true;
  151.    }
  152. }
  153.